package com.jjwxc.jwjskandriod.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;
import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.activity.BillboardActivity;
import com.jjwxc.jwjskandriod.activity.BookDetailsActivity;
import com.jjwxc.jwjskandriod.activity.MoreBookActivity;
import com.jjwxc.jwjskandriod.adapter.DerivativeViewHolder;
import com.jjwxc.jwjskandriod.adapter.TabAdapter;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.base.FFActivity;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.framework.imageloader.FFImageLoader;
import com.jjwxc.jwjskandriod.framework.refresh.XRefreshView;
import com.jjwxc.jwjskandriod.framework.refresh.recyclerview.BaseRecyclerAdapter;
import com.jjwxc.jwjskandriod.framework.utils.PreUtils;
import com.jjwxc.jwjskandriod.model.ChannelInRankResponse;
import com.jjwxc.jwjskandriod.model.ListWeekBookResopnse;
import com.jjwxc.jwjskandriod.readActivity.utils.UMConstant;
import com.jjwxc.jwjskandriod.widget.LinearLayouDecoration;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

public class DerivativeFragment extends Fragment implements View.OnClickListener {
    private View view;
    TabLayout tab_derivative_more;
    TabLayout tab_derivative_two;
    TabLayout tab_derivative_three;
    TabLayout tab_derivative_bang;
    ViewPager pager_derivative_bang;
    ViewPager pager_derivative_two;
    ViewPager pager_derivative_three;
    public static long lastRefreshTime;
    private RecyclerView rv_derivative_one;
    private DerivativeAdapter adapter;

    private TextView tv_title_more;
    private TextView tv_more_one;

    private LinearLayout ll_one;
    private LinearLayout ll_two;
    private LinearLayout ll_three;
    private LinearLayout ll_channel_two;
    private LinearLayout ll_channel_one;
    private LinearLayout ll_bg_one;
    private XRefreshView refreshView;
    private final String[] TAB_TITLES = new String[]{"月榜", "季榜", "半年榜", "年榜"};
    String key;
    String value;
    int mTab = 0;

    public DerivativeFragment(String key, String value) {
        this.key = key;
        this.value = value;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_derivative, container, false);
            setView();
            onListener();
            postDate();
            setListTabbang(tab_derivative_bang, getLayoutInflater(), TAB_TITLES);
            postDatebang(TAB_TITLES);
        }
        return view;
    }

    public void setView() {
        tab_derivative_more = view.findViewById(R.id.tab_derivative_more);
        tab_derivative_two = view.findViewById(R.id.tab_derivative_two);
        pager_derivative_two = view.findViewById(R.id.pager_derivative_two);
        pager_derivative_three = view.findViewById(R.id.pager_derivative_three);
        tab_derivative_three = view.findViewById(R.id.tab_derivative_three);
        rv_derivative_one = view.findViewById(R.id.rv_derivative_one);
        pager_derivative_bang = view.findViewById(R.id.pager_derivative_bang);
        tab_derivative_bang = view.findViewById(R.id.tab_derivative_bang);
        tv_title_more = view.findViewById(R.id.tv_title_more);
        tv_more_one = view.findViewById(R.id.tv_more_one);
        ll_one = view.findViewById(R.id.ll_one);
        ll_two = view.findViewById(R.id.ll_two);
        ll_three = view.findViewById(R.id.ll_three);
        ll_channel_one = view.findViewById(R.id.ll_channel_one);
        ll_channel_two = view.findViewById(R.id.ll_channel_two);
        ll_bg_one = view.findViewById(R.id.ll_bg_one);
        refreshView = view.findViewById(R.id.derivative_view);
        //设置不可滑动
        rv_derivative_one.setLayoutManager(new LinearLayoutManager(getActivity()) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        rv_derivative_one.addItemDecoration(new LinearLayouDecoration(getActivity(), LinearLayouDecoration.HORIZONTAL, R.dimen.line_rv, R.color.transparent));
        rv_derivative_one.getItemAnimator().setChangeDuration(0);// 通过设置动画执行时间为0来解决闪烁问题
        adapter = new DerivativeAdapter(getActivity());
        rv_derivative_one.setAdapter(adapter);

        refreshView.setPinnedTime(1000);
        refreshView.setMoveForHorizontal(true);
        refreshView.setPullRefreshEnable(true);
        refreshView.setPullLoadEnable(false);
        refreshView.setAutoLoadMore(false);
        refreshView.enableReleaseToLoadMore(false);
        refreshView.enableRecyclerViewPullUp(false);
        refreshView.enablePullUpWhenLoadCompleted(false);
        // 设置上次刷新的时间
        refreshView.restoreLastRefreshTime(lastRefreshTime);
    }

    public void onListener() {
        tv_title_more.setOnClickListener(this);
        tv_more_one.setOnClickListener(this);
        ll_one.setOnClickListener(this);
        ll_two.setOnClickListener(this);
        ll_three.setOnClickListener(this);
        refreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(boolean isPullDown) {
                postDate();
                setListTabbang(tab_derivative_bang, getLayoutInflater(), TAB_TITLES);
                postDatebang(TAB_TITLES);
            }

        });
    }

    @Override
    public void onResume() {
        super.onResume();
        if (PreUtils.getInt("app_bg", 0) == 0) {
            ll_bg_one.setBackgroundResource(R.mipmap.bg_vip_one);
            ll_channel_one.setBackgroundResource(R.mipmap.bgc_vip_one);
            ll_channel_two.setBackgroundResource(R.mipmap.bgc_vip_one);
        } else if (PreUtils.getInt("app_bg", 0) == 2) {
            ll_bg_one.setBackgroundResource(R.mipmap.bg_vip_two);
            ll_channel_one.setBackgroundResource(R.mipmap.bgc_vip_two);
            ll_channel_two.setBackgroundResource(R.mipmap.bgc_vip_two);
        } else if (PreUtils.getInt("app_bg", 0) == 1) {
            ll_bg_one.setBackgroundResource(R.mipmap.bg_vip_three);
            ll_channel_one.setBackgroundResource(R.mipmap.bgc_vip_three);
            ll_channel_two.setBackgroundResource(R.mipmap.bgc_vip_three);
        }else {
            ll_bg_one.setBackgroundResource(R.mipmap.bg_vip_moren);
            ll_channel_one.setBackgroundResource(R.mipmap.bgc_vip_moren);
            ll_channel_two.setBackgroundResource(R.mipmap.bgc_vip_moren);
        }
        switch (key) {
            case "1":
                MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANQING_WEEKNEW);
                MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANQING_BANGDAN1);
                MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANQING_BANGDAN2);
                MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANQING_BANGDAN3);
                break;
            case "2":
                MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_CHUNAI_WEEKNEW);
                MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_CHUNAI_BANGDAN1);
                MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_CHUNAI_BANGDAN2);
                MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_CHUNAI_BANGDAN3);
                break;
            case "3":
                MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANSHENG_WEEKNEW);
                MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANSHENG_BANGDAN1);
                MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANSHENG_BANGDAN2);
                MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANSHENG_BANGDAN3);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_title_more) {
            Intent intent = new Intent(getActivity(), MoreBookActivity.class);
            intent.putExtra("MORE_Url", Url.listThisWeekBornBook);
            intent.putExtra("MORE_Name", "本周上新");
            intent.putExtra("MORE_Key", "tab");
            intent.putExtra("MORE_Value", value);
            startActivity(intent);
            switch (key) {
                case "1":
                    MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANQING_WEEKNEW_MORE_CLICK);
                    break;
                case "2":
                    MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_CHUNAI_WEEKNEW_MORE_CLICK);
                    break;
                case "3":
                    MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANSHENG_WEEKNEW_MORE_CLICK);
                    break;
            }
        } else if (v.getId() == R.id.tv_more_one) {

            Intent intent = new Intent(getActivity(), MoreBookActivity.class);
            intent.putExtra("MORE_Url", Url.listHotBook);
            intent.putExtra("MORE_Name", "热门飙升");
            intent.putExtra("MORE_Key", "tab");
            intent.putExtra("MORE_Value", value);
            startActivity(intent);
            switch (key) {
                case "1":
                    MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANQING_HOT_MORE_CLICK);
                    break;
                case "2":
                    MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_CHUNAI_HOT_MORE_CLICK);
                    break;
                case "3":
                    MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANSHENG_HOT_MORE_CLICK);
                    break;
            }
        } else if (v.getId() == R.id.ll_one) {
            Intent intent = new Intent(getActivity(), MoreBookActivity.class);
            intent.putExtra("MORE_Url", Url.listRankBook);
            intent.putExtra("MORE_Name", oneName);
            intent.putExtra("MORE_Key", "derivative");
            intent.putExtra("MORE_Value", value);
            intent.putExtra("MORE_Channels", oneId);
            startActivity(intent);
            switch (key) {
                case "1":
                    MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANQING_BANGDAN1_MORE_CLICK);
                    break;
                case "2":
                    MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_CHUNAI_BANGDAN1_MORE_CLICK);
                    break;
                case "3":
                    MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANSHENG_BANGDAN1_MORE_CLICK);
                    break;
            }
        } else if (v.getId() == R.id.ll_two) {
            Intent intent = new Intent(getActivity(), MoreBookActivity.class);
            intent.putExtra("MORE_Url", Url.listRankBook);
            intent.putExtra("MORE_Name", twoName);
            intent.putExtra("MORE_Key", "derivative");
            intent.putExtra("MORE_Value", value);
            intent.putExtra("MORE_Channels", twoId);
            startActivity(intent);
            switch (key) {
                case "1":
                    MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANQING_BANGDAN2_MORE_CLICK);
                    break;
                case "2":
                    MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_CHUNAI_BANGDAN2_MORE_CLICK);
                    break;
                case "3":
                    MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANSHENG_BANGDAN2_MORE_CLICK);
                    break;
            }
        } else if (v.getId() == R.id.ll_three) {
            Intent intent = new Intent(getActivity(), BillboardActivity.class);
            intent.putExtra("mainTab", value);
            intent.putExtra("mTab", mTab);
            startActivity(intent);
            switch (key) {
                case "1":
                    MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANQING_BANGDAN3_MORE_CLICK);
                    break;
                case "2":
                    MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_CHUNAI_BANGDAN3_MORE_CLICK);
                    break;
                case "3":
                    MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANSHENG_BANGDAN3_MORE_CLICK);
                    break;
            }
        }
    }

    public void postDate() {
        ((FFActivity) getActivity()).post(Url.listThisWeekBornBook, null, ListWeekBookResopnse.class, new FFNetWorkCallBack<ListWeekBookResopnse>() {
            @Override
            public void onSuccess(ListWeekBookResopnse response) {
                if (response.getCode() == 200) {
                    setTabs(tab_derivative_more, getLayoutInflater(), response.getData());
                }
            }

            @Override
            public boolean onFail(ListWeekBookResopnse response) {
                return false;
            }
        }, "{\"limit\":10,\"tab\":\"" + value + "\"}");

        ((FFActivity) getActivity()).post(Url.listHotBook, null, ListWeekBookResopnse.class, new FFNetWorkCallBack<ListWeekBookResopnse>() {
            @Override
            public void onSuccess(ListWeekBookResopnse response) {
                if (response.getCode() == 200) {

                    adapter.setData(response.getData());
                }
            }

            @Override
            public boolean onFail(ListWeekBookResopnse response) {
                return false;
            }
        }, "{\"limit\":10,\"tab\":\"" + value + "\"}");

        ((FFActivity) getActivity()).post(Url.listChannelInRank, null, ChannelInRankResponse.class, new FFNetWorkCallBack<ChannelInRankResponse>() {
            @Override
            public void onSuccess(ChannelInRankResponse response) {
                refreshView.stopRefresh();
                if (response.getCode() == 200) {
                    if (response.getData().getFirstTab().size() <= 0) {
                        ll_channel_one.setVisibility(View.GONE);
                    } else {
                        setListTab(tab_derivative_two, getLayoutInflater(), response.getData().getFirstTab());
                        postDate(response.getData().getFirstTab());
                    }
                    if (response.getData().getSecondTab().size() <= 0) {
                        ll_channel_two.setVisibility(View.GONE);
                    } else {
                        setListTabThree(tab_derivative_three, getLayoutInflater(), response.getData().getSecondTab());
                        postDateThree(response.getData().getSecondTab());
                    }


                }
            }

            @Override
            public boolean onFail(ChannelInRankResponse response) {
                return false;
            }
        }, "{\"tab\":\"" + value + "\"}");


    }

    /**
     * 设置页卡显示效果
     *
     * @param tabLayout
     * @param inflater
     */
    private void setTabs(TabLayout tabLayout, LayoutInflater inflater, List<ListWeekBookResopnse.DataBean> list) {
        tabLayout.removeAllTabs();
        for (int i = 0; i < list.size(); i++) {
            TabLayout.Tab tab = tabLayout.newTab();
            View view = inflater.inflate(R.layout.allsuggest_item_book, null);
            // 使用自定义视图，目的是为了便于修改，也可使用自带的视图
            tab.setCustomView(view);
            ImageView img_grid_book = view.findViewById(R.id.img_grid_book);
            TextView txt_grid_book = view.findViewById(R.id.txt_grid_book);
            FFImageLoader.loadImage(getContext(), img_grid_book, list.get(i).getCoverImgUrl(), list.get(i).getBookId(), true);
            txt_grid_book.setText(list.get(i).getName());

            tabLayout.addTab(tab);
            int finalI = i;
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), BookDetailsActivity.class);
                    intent.putExtra("bookId", list.get(finalI).getBookId());
                    startActivity(intent);
                    switch (key) {
                        case "1":
                            MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANQING_WEEKNEW_CLICK);
                            break;
                        case "2":
                            MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_CHUNAI_WEEKNEW_CLICK);
                            break;
                        case "3":
                            MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANSHENG_WEEKNEW_CLICK);
                            break;
                    }
                }
            });
        }
    }


    /**
     * 设置页卡显示效果
     */
    private void setListTab(TabLayout tabLayout, LayoutInflater inflater, List<ChannelInRankResponse.DataBean.FirstTabBean> list) {
        tabLayout.removeAllTabs();
        for (int i = 0; i < list.size(); i++) {
            TabLayout.Tab tab = tabLayout.newTab();
            View view = inflater.inflate(R.layout.tab_item_current, null);
            // 使用自定义视图，目的是为了便于修改，也可使用自带的视图
            tab.setCustomView(view);
            TextView tvTitle = (TextView) view.findViewById(R.id.txt_stack_tab);
            tvTitle.setText(list.get(i).getName());
            tabLayout.addTab(tab);
        }
    }

    private String oneId;
    private String oneName;

    public void postDate(List<ChannelInRankResponse.DataBean.FirstTabBean> mList) {

        List<String> str = new ArrayList<>();
        ArrayList<Fragment> list = new ArrayList<>();
        for (int i = 0; i < mList.size(); i++) {
            str.add(i + "");
            list.add(new DerivativeInfoFragment(value, mList.get(i).getChannelId(), "one"));
        }
        oneId = mList.get(0).getChannelId();
        oneName = mList.get(0).getName();
        //适配器
        TabAdapter adapter = new TabAdapter(getChildFragmentManager(), str, list);
        pager_derivative_two.setAdapter(adapter);
        LinearLayout linearLayout = (LinearLayout) tab_derivative_two.getTabAt(0).getCustomView();
        TextView textView = (TextView) linearLayout.getChildAt(0);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        // 关联切换
        pager_derivative_two.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tab_derivative_two));
        tab_derivative_two.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                oneId = mList.get(tab.getPosition()).getChannelId();
                oneName = mList.get(tab.getPosition()).getName();
                int s = tab_derivative_two.getTabCount();
                for (int i = 0; i < s; i++) {
                    if (tab.getPosition() == i) {
                        LinearLayout linearLayout = (LinearLayout) tab_derivative_two.getTabAt(i).getCustomView();
                        TextView textView = (TextView) linearLayout.getChildAt(0);
                        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
                        textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    } else {
                        LinearLayout linearLayout = (LinearLayout) tab_derivative_two.getTabAt(i).getCustomView();
                        TextView textView = (TextView) linearLayout.getChildAt(0);
                        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                        textView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    }
                }
                // 取消平滑切换
                pager_derivative_two.setCurrentItem(tab.getPosition(), false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


    }

    /**
     * 设置页卡显示效果
     */
    private void setListTabThree(TabLayout tabLayout, LayoutInflater inflater, List<ChannelInRankResponse.DataBean.SecondTabBean> list) {
        tabLayout.removeAllTabs();
        for (int i = 0; i < list.size(); i++) {
            TabLayout.Tab tab = tabLayout.newTab();
            View view = inflater.inflate(R.layout.tab_item_current, null);
            // 使用自定义视图，目的是为了便于修改，也可使用自带的视图
            tab.setCustomView(view);
            TextView tvTitle = (TextView) view.findViewById(R.id.txt_stack_tab);
            tvTitle.setText(list.get(i).getName());
            tabLayout.addTab(tab);
        }
    }

    private String twoId;
    private String twoName;

    public void postDateThree(List<ChannelInRankResponse.DataBean.SecondTabBean> mList) {
        List<String> str = new ArrayList<>();
        ArrayList<Fragment> list = new ArrayList<>();
        for (int i = 0; i < mList.size(); i++) {
            str.add(i + "");
            list.add(new DerivativeInfoFragment(value, mList.get(i).getChannelId(), "two"));
        }
        twoId = mList.get(0).getChannelId();
        twoName = mList.get(0).getName();
        //适配器
        TabAdapter adapter = new TabAdapter(getChildFragmentManager(), str, list);
        pager_derivative_three.setAdapter(adapter);

        LinearLayout linearLayout = (LinearLayout) tab_derivative_three.getTabAt(0).getCustomView();
        TextView textView = (TextView) linearLayout.getChildAt(0);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));

        // 关联切换
        pager_derivative_three.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tab_derivative_three));
        tab_derivative_three.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                twoId = mList.get(tab.getPosition()).getChannelId();
                twoName = mList.get(tab.getPosition()).getName();
                int s = tab_derivative_three.getTabCount();
                for (int i = 0; i < s; i++) {
                    if (tab.getPosition() == i) {
                        LinearLayout linearLayout = (LinearLayout) tab_derivative_three.getTabAt(i).getCustomView();
                        TextView textView = (TextView) linearLayout.getChildAt(0);
                        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
                        textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    } else {
                        LinearLayout linearLayout = (LinearLayout) tab_derivative_three.getTabAt(i).getCustomView();
                        TextView textView = (TextView) linearLayout.getChildAt(0);
                        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                        textView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    }
                }
                // 取消平滑切换
                pager_derivative_three.setCurrentItem(tab.getPosition(), false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


    }

    /**
     * 设置页卡显示效果
     */
    private void setListTabbang(TabLayout tabLayout, LayoutInflater inflater, String[] list) {

        tabLayout.removeAllTabs();
        for (String s : list) {
            TabLayout.Tab tab = tabLayout.newTab();
            View view = inflater.inflate(R.layout.deriva_bang_list_item, null);
            // 使用自定义视图，目的是为了便于修改，也可使用自带的视图
            tab.setCustomView(view);
            TextView tvTitle = (TextView) view.findViewById(R.id.txt_allstack_tab);
            tvTitle.setText(s);
            tabLayout.addTab(tab);
        }
    }

    public void postDatebang(String[] mList) {
        List<String> str = new ArrayList<>();
        ArrayList<Fragment> list = new ArrayList<>();
        for (int i = 0; i < mList.length; i++) {
            str.add(i + "");
            list.add(new ListRankFragment(value, (i + 1) + ""));
        }
        //适配器
        TabAdapter adapter = new TabAdapter(getChildFragmentManager(), str, list);
        pager_derivative_bang.setAdapter(adapter);
        // 关联切换
        pager_derivative_bang.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tab_derivative_bang));
        tab_derivative_bang.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                // 取消平滑切换
                pager_derivative_bang.setCurrentItem(tab.getPosition(), false);
                mTab = tab.getPosition();
                switch (key) {
                    case "1":
                        if (tab.getPosition() == 0) {
                            MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANQING_BANGDAN3_MONTH_CLICK);
                        } else if (tab.getPosition() == 1) {
                            MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANQING_BANGDAN3_QUARTER_CLICK);
                        } else if (tab.getPosition() == 2) {
                            MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANQING_BANGDAN3_HALFYEAR_CLICK);
                        } else if (tab.getPosition() == 3) {
                            MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANQING_BANGDAN3_YEAR_CLICK);
                        }
                        break;
                    case "2":
                        if (tab.getPosition() == 0) {
                            MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_CHUNAI_BANGDAN3_MONTH_CLICK);
                        } else if (tab.getPosition() == 1) {
                            MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_CHUNAI_BANGDAN3_QUARTER_CLICK);
                        } else if (tab.getPosition() == 2) {
                            MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_CHUNAI_BANGDAN3_HALFYEAR_CLICK);
                        } else if (tab.getPosition() == 3) {
                            MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_CHUNAI_BANGDAN3_YEAR_CLICK);
                        }
                        break;
                    case "3":
                        if (tab.getPosition() == 0) {
                            MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANSHENG_BANGDAN3_MONTH_CLICK);
                        } else if (tab.getPosition() == 1) {
                            MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANSHENG_BANGDAN3_QUARTER_CLICK);
                        } else if (tab.getPosition() == 2) {
                            MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANSHENG_BANGDAN3_HALFYEAR_CLICK);
                        } else if (tab.getPosition() == 3) {
                            MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANSHENG_BANGDAN3_YEAR_CLICK);
                        }
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


    }


    public class DerivativeAdapter extends BaseRecyclerAdapter<DerivativeViewHolder> {
        private Context context;


        private List<ListWeekBookResopnse.DataBean> mData = new ArrayList<>();


        public DerivativeAdapter(Context cnt) {
            this.context = cnt;

        }

        public void addData(List<ListWeekBookResopnse.DataBean> data) {
            if (data != null) {
                mData.addAll(data);
                notifyDataSetChanged();
            }
        }

        public void setData(List<ListWeekBookResopnse.DataBean> data) {
            this.mData.clear();
            addData(data);
        }

        /**
         * 创建ViewHolder
         */
        @Override
        public DerivativeViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {

            View mView = LayoutInflater.from(context).inflate(R.layout.derivative_item_list, parent, false);
            DerivativeViewHolder viewHolder_text = new DerivativeViewHolder(mView);
            viewHolder_text.setIsRecyclable(false);
            return viewHolder_text;

        }

        @Override
        public DerivativeViewHolder getViewHolder(View view) {
            return new DerivativeViewHolder(view);
        }


        /**
         * 绑定数据：可以直接拿到已经绑定控件的Viewholder对象
         */
        @SuppressLint({"SetTextI18n", "ClickableViewAccessibility"})
        @Override
        public void onBindViewHolder(final DerivativeViewHolder holder, @SuppressLint("RecyclerView") final int position, boolean isItem) {
            holder.tv_title.setText(mData.get(position).getName());
            holder.tv_author.setText(mData.get(position).getAuthor());
            if (position < 3) {
                holder.img_hot.setVisibility(View.VISIBLE);
            } else {
                holder.img_hot.setVisibility(View.GONE);
            }
            holder.cl_derivate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), BookDetailsActivity.class);
                    intent.putExtra("bookId", mData.get(position).getBookId());
                    startActivity(intent);
                    switch (key) {
                        case "1":
                            MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANQING_HOT_CLICK);
                            break;
                        case "2":
                            MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_CHUNAI_HOT_CLICK);
                            break;
                        case "3":
                            MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANSHENG_HOT_CLICK);
                            break;
                    }
                }
            });


        }

        @Override
        public int getAdapterItemViewType(int position) {
            return position;
        }


        @Override
        public int getAdapterItemCount() {
            return mData.size();
        }


    }
}
